/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


/* global c5, C5ModalTitle */

jQuery.extend({
    showMessage: function (options) {
        var opts = {
            content: 'empty',
            title: c5_modal_title,
            height: 120,
            width: 500,
            buttons: [{
                text: c5_modal_comfirm,
                'callback': function () {
                },
                'class': 'btn-green btn-sm',
                'dismiss': true,
                'id': "",
                'url': 'javascript:void(0)',
                'attribute': ''
            }],
            class: '',
            refresh: false,
            redirect: "",
            time: 1.5,
            afterLoad: null,
            close: false,
            className: '',
            multiple: false,
            before: function () {

            }
        };

        var opts = $.extend(opts, options);
        if (!opts.multiple) {
            if ($('.modal').hasClass('alert-message') !== false) {

                $('.alert-message').remove();

            }

            if ($('.modal-backdrop').hasClass('fade')) {

                $('.modal-backdrop').remove();

            }
        }


        var buttons = '';
        var $footer = $('<div class="modal-footer">');
        console.log(opts)
        for (var index in opts.buttons) {

            var btn = opts.buttons[index];

            var btn_id = btn.id === '' || typeof (btn.id) === 'undefined' ? "alert-btn-" + (index + 100 + Math.floor(Math.random() * 1000000)) : btn.id;
            var sBtn = '<a id="' + btn_id + '" href="' + (typeof (btn.url) === 'undefined' ? 'javascript:void(0)' : btn.url) + '"' + btn.attribute + ' class="btn ' + (typeof (btn.class) === 'undefined' ? 'btn-info' : btn.class) + ' " ' + (btn.dismiss || typeof (btn.dismiss) === 'undefined' ? 'data-dismiss="modal"' : '') + ' >' + btn.text + '</a>';
            var $btn = $(sBtn);
            if (typeof (btn.callback) === 'function') {
                $btn.click(btn.callback);
            }
            $footer.append($btn);
            console.log(index)
        }


        var $template = $('<div class="modal-dialog"><div class="modal-middle"><div class="modal-content"><div class="modal-header">' + opts.title + '</div><div class="modal-body text-center"></div></div></div></div>');

        if (opts.buttons.length > 0) {
            $template.find('.modal-content').append($footer);
        }

        if (close) {
            if (opts.className == "com-newalert") {
                $template.find('.modal-header').prepend('<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span class="iconfont">&#xe849;</span></button>');
            } else {
                $template.find('.modal-header').prepend('<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>');
            }
        }

        var $alert = $('<div>').addClass('modal fade alert-message ' + opts.className);

        $alert.data('backdrop', 'static').attr('aria-hidden', true).attr('dialog');

        $alert.append($template);

        var $body = $alert.find(".modal-body");
        $body.attr('class', 'modal-body text-center ' + opts.class);
        $body.css('height', opts.height).css('vertical-align', 'middle').css('display', 'table-cell');
        $body.css('width', opts.width);
        $body.html('<div>' + opts.content + '</div>');


        $('body').append($alert);

        opts.before($alert);

        $alert.modal('show');

        if (opts.refresh !== false) {
            setTimeout(function () {
                if (opts.redirect) {
                    window.location.href = opts.redirect;
                } else {
                    window.location.reload();
                }
            }, opts.time * 1000);
        }

        if (typeof (opts.afterLoad) === 'function') {
            opts.afterLoad();
        }
        return $alert;
    },
    showSuccessMessage: function (options) {

        // var opts = {class: 'text-success'};

        var opts = {class: ''};

        if (typeof (options) === 'string') {

            opts.content = options;

        } else {

            jQuery.extend(opts, options);
        }

        return jQuery.showMessage(opts);

    },
    showErrorMessage: function (options) {

        var opts = {class: 'text-error'};

        if (typeof (options) === 'string') {

            opts.content = options;

        } else {

            jQuery.extend(opts, options);
        }

        return jQuery.showMessage(opts);


    }
});
